package com.xinghai.system.service.impl;


import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.xinghai.common.utils.DateUtils;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.system.mapper.QdPayMapper;
import com.xinghai.system.domain.QdPay;
import com.xinghai.system.service.IQdPayService;

/**
 * 对账单Service业务层处理
 *
 * @author xinghai
 * @date 2024-12-04
 */
@Service
public class QdPayServiceImpl extends ServiceImpl<QdPayMapper, QdPay>
        implements IQdPayService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询对账单列表
     *
     * @param qdPay 对账单
     * @return 对账单集合
     */
    @Override
    public List<QdPay> list(QdPay qdPay) {
        QueryWrapper<QdPay> queryWrapper = new QueryWrapper<>();
        if (qdPay.getStatus()!= null  && !qdPay.getStatus().isEmpty()){
            queryWrapper.eq("status", qdPay.getStatus());
        }
        if (qdPay.getBuyerBankInfo()!= null  && !qdPay.getBuyerBankInfo().isEmpty()){
            queryWrapper.like("buyer_bank_info", qdPay.getBuyerBankInfo());
        }
        if (qdPay.getBuyerBankNo()!= null  && !qdPay.getBuyerBankNo().isEmpty()){
            queryWrapper.like("buyer_bank_no", qdPay.getBuyerBankNo());
        }
        if (qdPay.getBuyerBankName()!= null  && !qdPay.getBuyerBankName().isEmpty()){
            queryWrapper.like("buyer_bank_name", qdPay.getBuyerBankName());
        }
        if (qdPay.getSellerBankInfo()!= null  && !qdPay.getSellerBankInfo().isEmpty()){
            queryWrapper.like("seller_bank_info", qdPay.getSellerBankInfo());
        }
        if (qdPay.getSellerBankNo()!= null  && !qdPay.getSellerBankNo().isEmpty()){
            queryWrapper.like("seller_bank_no", qdPay.getSellerBankNo());
        }
        if (qdPay.getSellerBankName()!= null  && !qdPay.getSellerBankName().isEmpty()){
            queryWrapper.like("seller_bank_name", qdPay.getSellerBankName());
        }
        if (qdPay.getSellerUserName()!= null  && !qdPay.getSellerUserName().isEmpty()){
            queryWrapper.like("seller_user_name", qdPay.getSellerUserName());
        }
        if (qdPay.getSellerUserPhone()!= null  && !qdPay.getSellerUserPhone().isEmpty()){
            queryWrapper.like("seller_user_phone", qdPay.getSellerUserPhone());
        }
        if (qdPay.getSellerBalance()!= null  && !qdPay.getSellerBalance().isEmpty()){
            queryWrapper.eq("seller_balance", qdPay.getSellerBalance());
        }
        if (qdPay.getBuyerUserName()!= null  && !qdPay.getBuyerUserName().isEmpty()){
            queryWrapper.like("buyer_user_name", qdPay.getBuyerUserName());
        }
        if (qdPay.getBuyerUserPhone()!= null  && !qdPay.getBuyerUserPhone().isEmpty()){
            queryWrapper.like("buyer_user_phone", qdPay.getBuyerUserPhone());
        }
        if (qdPay.getBuyerBalance()!= null  && !qdPay.getBuyerBalance().isEmpty()){
            queryWrapper.eq("buyer_balance", qdPay.getBuyerBalance());
        }
        if (qdPay.getParams().get("beginCreateTime")!= null && !"".equals(qdPay.getParams().get("beginCreateTime"))
                &&qdPay.getParams().get("endCreateTime")!= null && !"".equals(qdPay.getParams().get("endCreateTime"))){
            queryWrapper.between("create_time", qdPay.getParams().get("beginCreateTime"),qdPay.getParams().get("endCreateTime"));
        }
        if (qdPay.getBuyerUserId()!= null){
            queryWrapper.eq("buyer_user_id", qdPay.getBuyerUserId());
        }
        if (qdPay.getBuyerUserIds()!= null){
            queryWrapper.apply("FIND_IN_SET(buyer_user_id,'"+qdPay.getBuyerUserIds()+"')");
        }
        if (qdPay.getSellerUserIds()!= null){
            queryWrapper.apply("FIND_IN_SET(seller_user_id,'"+qdPay.getSellerUserIds()+"')");
        }
        if (qdPay.getSellerUserId()!= null){
            queryWrapper.eq("seller_user_id", qdPay.getSellerUserId());
        }
        if (qdPay.getUserIdsBuyer()!= null && !qdPay.getUserIdsBuyer().isEmpty()){
            queryWrapper.apply("FIND_IN_SET(buyer_user_id,get_child_list('"+qdPay.getUserIdsBuyer()+"'))");
        }
        if (qdPay.getUserIdsSeller()!= null && !qdPay.getUserIdsSeller().isEmpty()){
            queryWrapper.apply("FIND_IN_SET(seller_user_id,get_child_list('"+qdPay.getUserIdsSeller()+"'))");
        }
        return this.list(queryWrapper);
    }

    /**
     * 删除对账单信息
     * @param id 需要删除的数据ID
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    public boolean delete(Long id, boolean flag) {
        if (flag){
            QdPay qdPay = new QdPay();
            qdPay.setId(id);
            qdPay.setDelFlag(GenConstants.DELETE_FLAG_YES);
            return this.updateById(qdPay);
        }
        return this.removeById(id);
    }

    /**
     * 批量删除对账单信息
     *
     * @param ids 需要删除的数据ID集合
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteBatch(Long[] ids, boolean flag) {
        if (flag){
            List<QdPay> collect = Arrays.stream(ids).map(m -> {
                QdPay qdPay = new QdPay();
                qdPay.setId(m);
                qdPay.setDelFlag(GenConstants.DELETE_FLAG_YES);
                return qdPay;
            }).collect(Collectors.toList());
            return this.updateBatchById(collect);
        }
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 导入对账单信息
     *
     * @param list 对账单信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<QdPay> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            QdPayMapper qdPayMapper = sqlSession.getMapper(QdPayMapper.class);
            list.forEach(qdPayMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    public List<Map<String, Object>> pushListInfo(String date, String loginId) {
        return baseMapper.pushListInfo(date,loginId);
    }
}
